package net.vistiyos.android;

import java.util.ArrayList;

import net.vistiyos.R;
import net.vistiyos.busquedas.Buscar;
import net.vistiyos.modelo.Serie;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class SearchActivity extends Activity implements OnItemClickListener{
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.busquedas);
	    ListView lista = (ListView) findViewById(R.id.listaseries);
	    SerieAdapter adaptador = new SerieAdapter(Buscar.obtenerUltimaBusqueda());
	    lista.setOnItemClickListener(this);
	    lista.setAdapter(adaptador);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case android.R.id.home:
	            // app icon in action bar clicked; go home
	            Intent intent = new Intent(this, PanelAdministracion.class);
	            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            startActivity(intent);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	class SerieAdapter extends ArrayAdapter<Serie> {
    	
    	private ArrayList<Serie> lista;
		SerieAdapter(ArrayList<Serie> lista) {
			super(SearchActivity.this, R.layout.row, lista);
			Log.v("SearchActivity.SerieAdapter",Integer.toString(lista.size()));
			this.lista = lista;
		}
		
		public View getView(int position, View convertView,ViewGroup parent) {
			LayoutInflater inflater=SearchActivity.this.getLayoutInflater();
			View row=inflater.inflate(R.layout.row, parent,	false);
			TextView label=(TextView)row.findViewById(R.id.label);
			label.setText(lista.get(position).getTitulo());
			return(row);
		}
    }

	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		ArrayList<Serie> series = Buscar.obtenerUltimaBusqueda();
		Intent obtenerSerie = new Intent(this,SerieActivity.class);
		String serieID = series.get(position).getIdSerie();
		obtenerSerie.putExtra("idSerie", serieID);
		startActivity(obtenerSerie);
	}
}
